<?php
namespace Common\Service;

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;
use Think\Log;

class Qiniu
{
    private static $_accessKey;
    private static $_secretKey;
    private static $_domain;
    private static $_uploadDomain;
    private static $_bucket;
    private static $_auth;

    private static function __init()
    {
        SELF::$_accessKey = C('qiniu_config')['accessKey'];
        SELF::$_secretKey = C('qiniu_config')['secretKey'];
        SELF::$_domain = C('qiniu_config')['domain'];
        SELF::$_uploadDomain = C('qiniu_config')['clientUploadUrl'];
        SELF::$_bucket = C('qiniu_config')['bucket'];

        if (!SELF::$_auth) {
            vendor('Qiniu.autoload');
            SELF::$_auth = new Auth(SELF::$_accessKey, SELF::$_secretKey);
        }
    }

    /**
     * 获取token
     * @return string
     */
    private static function getToken($key)
    {
        SELF::__init();
        $token = SELF::$_auth->uploadToken(SELF::$_bucket, $key);
        return $token;
    }

    /**
     * 返回token，url
     * @param int $num 生成token的个数
     * @return array
     */
    public static function getUpimageInfo($num = 1)
    {
        if ($num > 1) {
            for ($i = 0; $i <= $num - 1; $i++) {
                $key = md5(time() . $i);
                $token[$i]['token'] = SELF::getToken($key);
                $token[$i]['key'] = $key;
            }
        } else {
            $key = md5(time());
            $token['token'] = SELF::getToken($key);
            $token['key'] = $key;
        }

        $ret = [
            'token' => $token,
            'domain' => SELF::$_domain,
            'upDomain' => SELF::$_uploadDomain
        ];
        return $ret;
    }

    /**
     * 获取某一个空间的文件资源
     * @return array
     */
    public static function getList()
    {
        SELF::__init();
        $bkt = new BucketManager(SELF::$_auth);
        $lst = $bkt->listFiles(SELF::$_bucket);
        return $lst;
    }

    /**
     * 删除空间文件
     * @param $bucket
     * @param $key
     * @return mixed
     */
    public static function delImage($bucket, $key)
    {
        SELF::__init();
        $bkt = new BucketManager(SELF::$_auth);
        $ret = $bkt->delete($bucket, $key);
        return $ret;
    }

    /**
     * 上传图片
     * @param $filepath
     * @param $key
     * @return mixed
     */
    public static function upload($filepath, $key)
    {
        $token = SELF::getToken();
        // 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传。
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filepath);
        // $err->getResponse()->error;//获取错误信息
        // $err->getResponse()->statusCode;//获取错误code(614表示文件已经存在)
        if ($err !== null) {
            Log::write('七牛上传图片异常：' . $err);
            if (is_object($err)) {
                Log::write('七牛上传图片异常：' . $err->getResponse()->error . '---' . $err->getResponse()->statusCode);
                return $err->getResponse()->error;
            } else {
                Log::write('七牛上传图片异常：' . $err);
                return $err;
            }
        } else {
            return $ret;
        }
    }
}